<template>
    <h2 style="text-align: center;">添加借款信息</h2>
    <table class="table-border" border="1">
        <tr>
            <td>借款名称</td>
            <td>
                <input type="text" placeholder="请输入借款名称" v-model="info.BM_Name" />
            </td>
        </tr>
        <tr>
            <td>借款年限</td>
            <td>
                <input type="text" placeholder="请输入借款年限" v-model="info.BM_Year" />
            </td>
        </tr>
        <tr>
            <td>借款最高额度</td>
            <td>
                <input type="text" placeholder="请输入借款最高额度" v-model="info.BM_Money" />
            </td>
        </tr>
        <tr>
            <td>借款介绍</td>
            <td>
                <input type="text" placeholder="请输入借款介绍" v-model="info.BM_Info" />
            </td>
        </tr>
        <tr>
            <td>借款利息</td>
            <td>
                <select v-model="info.BMI_Id">
                    <option value="0">--请选择--</option>
                    <option v-for="item in Inserestinfo" :value="item.BMI_Id">
                        {{ item.BMI_Year }}年利率为{{ item.BMI_Interest }}%</option>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center;">
                <input type="button" class="btn btn-success" value="保存" @click=Add />
            </td>
        </tr>
    </table>
</template>


<script setup lang="ts">
import { ref, onMounted } from "vue";
import axios from "axios";
import { useRouter } from 'vue-router';

const router = useRouter();
const info = ref
    ({
        BM_Name: '',
        BM_Year: '',
        BM_Money: '',
        BM_Info: '',
        BMI_Id: 0,
    });

const Inserestinfo = ref([{
    BMI_Id: 0,
    BMI_Year: 0,
    BMI_Interest: 0,
}])

onMounted(() => {
    LoadInterst()
});

const LoadInterst = () => {
    axios({
        url: 'https://localhost:44387/api/Borrows/ShowInterestInfo',
        method: 'get'
        
    })
        .then(res => {
            console.log(res.data);
            Inserestinfo.value = res.data;
        })
        .catch(err => {
            console.log(err);
        })
}

const Add = () => {
    //验证非空
    if (info.value.BM_Name == "") {
        alert('借款名称不能为空');
        return;
    }
    axios({
        url: 'https://localhost:44387/api/Borrows/AddBorrowinfo',
        method: 'post',
        data: info.value
    })
        .then(res => {
            if (res.data > 0) {
                alert('借款项目信息保存成功');
                router.push('/ShowBorow');
                return;
            }
            else {
                alert('借款项目信息保存失败');
            }
        })
        .catch(err => {
            console.log(err);
        })
}

</script>